package com.das.mechanic_base.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class X3NumberTextWatcher implements TextWatcher {

    EditText editText;

    public X3NumberTextWatcher(EditText editText) {
        this.editText = editText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        editText.removeTextChangedListener(this);

        String value = editText.getText().toString();

        if (iOnGetNumber != null) {
            iOnGetNumber.iOnGetNumber(value);
        }
        if (X3StringUtils.isEmpty(value)) {
            editText.addTextChangedListener(this);
            return;
        }
        String replace = value.replace(",", "");

        editText.setText(X3StringUtils.num2thousand(replace));
        editText.setSelection(X3StringUtils.num2thousand(replace).length());
        editText.addTextChangedListener(this);
    }

    IOnGetNumber iOnGetNumber;

    public void setiOnGetNumber(IOnGetNumber iOnGetNumber) {
        this.iOnGetNumber = iOnGetNumber;
    }

    public interface IOnGetNumber {
        void iOnGetNumber(String value);
    }
}
